package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.AttachmentView;
import com.molyfun.parents.fragment.dialog.CommonInputDialogFragment;
import com.molyfun.parents.rest.bean.MediaobjectlistBean;
import com.molyfun.parents.rest.bean.NewSignUpSubmitInfoRequest;
import com.molyfun.parents.rest.bean.SignUpEvaluateResponse;
import com.molyfun.parents.utils.Pair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NewTeamSheetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;

    private static final int TYPE_SINGLE_CHOOSE = 1000;
    private static final int TYPE_MULT_CHOOSE = 1001;
    private static final int TYPE_ATTACHMENT_UPLOAD = 1002;

    //选中的结果
    private HashMap<String, ArrayList<String>> choiceIds = new HashMap<>();
    //题目
    private List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> mList;
    //素材类型
    private HashMap<String, Pair<String, ArrayList<MediaobjectlistBean>>> mediaobjectlistBeanList = new HashMap<>();

    private AttachmentView.AttachmentAddCallback mAttachmentAddCallback;
    private NewSignUpSubmitInfoRequest.ResultBean[] mResultList;//存储选择后的结果
    private FragmentManager mFragmentManager;

    public NewTeamSheetAdapter(Context context, AttachmentView.AttachmentAddCallback attachmentAddCallback, FragmentManager fragmentManager) {
        this.mContext = context;
        this.mAttachmentAddCallback = attachmentAddCallback;
        this.mFragmentManager = fragmentManager;
    }

    public void refreshData(List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> records) {
        this.mList = records;
        mResultList = new NewSignUpSubmitInfoRequest.ResultBean[mList.size()];
        notifyDataSetChanged();
    }

    public void clearAll() {
        if (mList != null) {
            mList.clear();
            notifyDataSetChanged();
        }
    }

    public void addAttachment(MediaobjectlistBean mediaobjectlistBean) {
        this.mediaobjectlistBeanList.get(mCurrentRequestMediaId).second.add(mediaobjectlistBean);
        notifyDataSetChanged();
    }

    public void addAll(List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> list) {
        this.mList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SignUpEvaluateSheetHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.layout_sign_up_evaluate, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        SignUpEvaluateSheetHolder mSignUpEvaluateSheetViewHolder = (SignUpEvaluateSheetHolder) holder;
        mSignUpEvaluateSheetViewHolder.mAnswerContainer.removeAllViews();
        switch (getItemViewType(position)) {
            case TYPE_SINGLE_CHOOSE:
            case TYPE_MULT_CHOOSE:
                final SignUpEvaluateResponse.DataBean.SheetRecordsBean recordsBean = mList.get(position);
                final String type = recordsBean.getType();//"0"单选，"1"多选 '2'填空
                if (TextUtils.isEmpty(type) || type.equals("0")) {
                    mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText((position + 1) + "." + recordsBean.getContent() + "(单选)");
                } else {
                    mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText((position + 1) + "." + recordsBean.getContent() + "(多选)");
                }
                if (recordsBean.getOption() != null && recordsBean.getOption().size() > 0) {
                    int optionNum = 0;
                    for (final SignUpEvaluateResponse.DataBean.SheetRecordsBean.OptionBean optionBean : recordsBean.getOption()) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.view_include_choose_info, null, false);
                        final ImageView questionState = (ImageView) view.findViewById(R.id.question_check_state);
                        TextView questionText = (TextView) view.findViewById(R.id.question_text);
                        questionText.setText(optionBean.getItem());
                        mSignUpEvaluateSheetViewHolder.mAnswerContainer.addView(view);
//                        if (type.equals("1")) {
                            if (choiceIds.containsKey(recordsBean.getId()) && choiceIds.get(recordsBean.getId()).contains(optionBean.getId())) {
                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                            } else {
                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
                            }
//                        } else {
//                            if (choiceIds.get(recordsBean.getId()).contains(optionBean.getId())) {
//                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
//                            } else {
//                                questionState.setImageResource(R.mipmap.ic_pay_checkbox_normal);
//                            }
//                        }

                        questionState.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (type.equals("0")) {
                                    questionState.setImageResource(R.mipmap.ic_pay_checkbox_choosed);
                                    choiceIds.put(recordsBean.getId(), new ArrayList<String>(){{
                                        add(optionBean.getId());
                                    }
                                    });
                                } else {

                                    if(choiceIds.containsKey(recordsBean.getId())){
                                        if(choiceIds.get(recordsBean.getId()).contains(optionBean.getId())){
                                            choiceIds.get(recordsBean.getId()).remove(optionBean.getId());
                                        }else{
                                            choiceIds.get(recordsBean.getId()).add(optionBean.getId());
                                        }
                                    }else{
                                        choiceIds.put(recordsBean.getId(), new ArrayList<String>(){{
                                            add(optionBean.getId());
                                        }});
                                    }
                                }
                                notifyItemChanged(position);
                            }
                        });
                        optionNum++;
                    }
                }
                break;
            case TYPE_ATTACHMENT_UPLOAD:
                final SignUpEvaluateResponse.DataBean.SheetRecordsBean mediaAttachmentBean = mList.get(position);
                mSignUpEvaluateSheetViewHolder.mChoiceQuestion.setText((position + 1) + "." + mediaAttachmentBean.getContent());
                View attachmentFill = LayoutInflater.from(mContext).inflate(R.layout.layout_new_team_sheet_fill_attachment, null, false);
                AttachmentView attachmentView = (AttachmentView) attachmentFill.findViewById(R.id.new_team_sheet_pic_upload);
                if(mediaobjectlistBeanList.containsKey(mediaAttachmentBean.getId()) && mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).second != null){
                    attachmentView.refreshList(mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).second);
                }else{
                    mediaobjectlistBeanList.put(mediaAttachmentBean.getId(), new Pair("", new ArrayList<MediaobjectlistBean>()));
                }
                attachmentView.setAttachmentDelCallback(new AttachmentView.AttachmentDelCallback() {
                    @Override
                    public void onAttachmentDel(MediaobjectlistBean mediaobjectlistBean) {
//                        mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).second.remove(mediaobjectlistBean);
                        notifyDataSetChanged();
                    }
                });
                attachmentView.setAttachmentAddCallback(new AttachmentView.AttachmentAddCallback() {
                    @Override
                    public void onAttachmentAdd() {
                        if(mAttachmentAddCallback != null){
                            mCurrentRequestMediaId = mediaAttachmentBean.getId();
                            mAttachmentAddCallback.onAttachmentAdd();
                        }
                    }
                });
                final TextView attachmentContent = (TextView) attachmentFill.findViewById(R.id.student_info_name);
                if(!TextUtils.isEmpty(mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).first)){
                    attachmentContent.setText(mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).first);
                }
                attachmentContent.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new CommonInputDialogFragment.CommonInputDialogBuilder(mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).first)
                                .setOnCommonInputCallback(new CommonInputDialogFragment.OnCommonInputCallback() {
                                    @Override
                                    public void onInputConfirm(String content) {
                                        attachmentContent.setText(content);
                                        mediaobjectlistBeanList.get(mediaAttachmentBean.getId()).first = content;
                                        notifyDataSetChanged();
                                    }
                                })
                                .build(mFragmentManager.beginTransaction());
                    }
                });

                mSignUpEvaluateSheetViewHolder.mAnswerContainer.addView(attachmentFill);

                break;
        }

    }

    private String mCurrentRequestMediaId = "";

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position).getType().equals("0")) {
            return TYPE_SINGLE_CHOOSE;
        } else if (mList.get(position).getType().equals("1")){
            return TYPE_MULT_CHOOSE;
        } else{
            return TYPE_ATTACHMENT_UPLOAD;
        }
    }

    //获取选择题答案和素材答案
    public List<NewSignUpSubmitInfoRequest.ResultBean> getInputResult(){
        List<NewSignUpSubmitInfoRequest.ResultBean> result = new ArrayList<>();

        for(String questionId : choiceIds.keySet()){
            NewSignUpSubmitInfoRequest.ResultBean resultBean = new NewSignUpSubmitInfoRequest.ResultBean();
            resultBean.setId(questionId);
            List<NewSignUpSubmitInfoRequest.OptionListBean> optionListBeans = new ArrayList<>();
            for(String optionId : choiceIds.get(questionId)){
                NewSignUpSubmitInfoRequest.OptionListBean optionListBean = new NewSignUpSubmitInfoRequest.OptionListBean();
                optionListBean.setId(optionId);
                optionListBeans.add(optionListBean);
            }
            resultBean.setOptionlist(optionListBeans);
            result.add(resultBean);
        }

        for(String questionId : mediaobjectlistBeanList.keySet()){
            NewSignUpSubmitInfoRequest.ResultBean resultBean = new NewSignUpSubmitInfoRequest.ResultBean();
            resultBean.setId(questionId);
            List<NewSignUpSubmitInfoRequest.OptionListBean> optionListBeans = new ArrayList<>();
            NewSignUpSubmitInfoRequest.OptionListBean optionListBean = new NewSignUpSubmitInfoRequest.OptionListBean();
            optionListBean.setContent(mediaobjectlistBeanList.get(questionId).first);
            optionListBean.setUrlList(mediaobjectlistBeanList.get(questionId).second);
            optionListBeans.add(optionListBean);
            resultBean.setOptionlist(optionListBeans);
            result.add(resultBean);
        }

        return result;
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

}
